package com.ruoyi.customerPool.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.customer.common.DisCustomerRequestBo;
import com.ruoyi.customer.domain.Customer;
import com.ruoyi.customer.domain.bo.CustomerPoolPullBo;
import com.ruoyi.customer.domain.vo.CustomerImportVo;
import com.ruoyi.customerPool.domain.bo.CustomerPoolBo;
import com.ruoyi.customerPool.domain.bo.CustomerReturn;
import com.ruoyi.customerPool.domain.bo.DeptBo;
import com.ruoyi.customerPool.domain.bo.UserBo;
import com.ruoyi.customerPool.domain.vo.CustomerPoolVo;
import com.ruoyi.customerPool.domain.vo.UserVo;
import com.ruoyi.customerPool.service.ICustomerPoolService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequiredArgsConstructor
@RestController
@RequestMapping("/customerPool")
public class CustomerPoolController extends BaseController {

    private final ICustomerPoolService customerPoolService;

    /**
     * 客户池（不区分本级、下级）
     */
    //@SaCheckPermission("customer:customerPool:manageQuery")
    @GetMapping("/customerPoolList")
    public TableDataInfo<CustomerPoolVo> queryPageCustomerPoolList(CustomerPoolBo bo, PageQuery pageQuery) {
        return customerPoolService.queryPageCustomerPoolList(bo, pageQuery);
    }


    /**
     * 上级数据
     */
    //@SaCheckPermission("customer:customerPool:manageQuery")
    @GetMapping("/superiorCustomerList")
    public TableDataInfo<CustomerPoolVo> querySuperiorCustomerList(CustomerPoolBo bo, PageQuery pageQuery) {
        return customerPoolService.querySuperiorCustomerList(bo, pageQuery);
    }

    /**
     * 用户信息
     */
    @GetMapping("/getUserList")
    public R<List<UserVo>> getUserList(UserBo userBo) {
        return R.ok(customerPoolService.getUsrList(userBo));
    }

    /**
     * 当前登录人下级部门信息
     */
    @GetMapping("/getDeptList")
    public R<List<SysDept>> getDeptList(DeptBo dept) {
        return R.ok(customerPoolService.getDeptList(dept));
    }

    /**
     * 部门信息
     */
    @GetMapping("/getDeptInfoList")
    public R<List<SysDept>> getDeptInfoList(SysDept dept) {
        return R.ok(customerPoolService.getDeptInfoList(dept));
    }

    /**
     * 分配客户给部门池
     */
    @PostMapping("/allotCustomerToDept")
    @RepeatSubmit
    public R<Void> allotCustomerToDept(@RequestBody DisCustomerRequestBo requestBo) {
        customerPoolService.allotCustomerToDept(requestBo);
        return R.ok();
    }

    /**
     * 下载导入客户模板
     */
    //@SaCheckPermission("customer:myCustomer:export")
    @RequestMapping(value = "/importTemplate")
    public R<Void> downloadTemplate() {
        return R.ok(customerPoolService.downLoad());
    }

    /**
     * 导出客户信息列表
     */
    //@SaCheckPermission("customer:myCustomer:export")
    @Log(title = "客户信息", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> customerImport(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        // 解析数据
        String result = "未读取到数据，请正确填写数据";
        List<CustomerImportVo> customerImportVos = ExcelUtil.importExcel(file.getInputStream(), CustomerImportVo.class);
        if (!CollectionUtils.isEmpty(customerImportVos)) {
            result = customerPoolService.importCustomerData(customerImportVos);
        }
        return R.ok(result);
    }

    /**
     * 分配客户给用户
     */
    @PostMapping("/allotCustomerToUser")
    @RepeatSubmit
    public R<Void> allotCustomerToUser(@RequestBody DisCustomerRequestBo allotUserBo) {
        customerPoolService.allotCustomerToUser(allotUserBo);
        return R.ok();
    }

    /**
     * 客户退回
     */
    @PostMapping("/returnCustomer")
    @RepeatSubmit
    public R<Void> returnCustomer(@RequestBody CustomerReturn customerReturn) {
        customerPoolService.returnCustomer(customerReturn);
        return R.ok();
    }

    /**
     * 客户池拉取上级数据
     */
    @PostMapping("/deptPoolPull")
    @RepeatSubmit
    public R<Void> deptPoolPull(@RequestBody CustomerPoolPullBo customerPoolPullBo) {
        customerPoolService.deptPoolPull(customerPoolPullBo);
        return R.ok();
    }

    /**
     * 批量退回
     * @author: suntf
     * @date: 2023/8/28 18:41
     * @param customers
     * @return com.ruoyi.common.core.domain.R<java.lang.Void>
     */
    @SaCheckPermission("customer:pool:gotoTotalCustomer")
    @PostMapping("bathReturnTotalCutomer")
    @RepeatSubmit
    public R<Void> bathReturnTotalCutomer(@RequestBody Customer customers) {
        return customerPoolService.bathReturnTotalCutomer(customers);
    }

}
